<?php
/**
 * Created by PhpStorm.
 * User: 10467
 * Date: 2020/10/29
 * Time: 15:05
 */

namespace app\api\controller\Index;

use app\api\controller\AppController;

use app\api\model\Index\VideoUser;
use app\api\model\Sns\SnsModel;
use app\api\model\User\UserModel;
use app\api\model\VideoUser\VideoUserCommentModel;
use app\api\model\VideoUser\VideoUserLikeModel;
use app\api\model\VideoUser\VideoUserModel;
use app\Request;
use app\Service\UserService;
use app\Service\VideoService;
use app\Utils\Util;

use app\Utils\ImageUtil;
use app\Utils\WechatPayUtil;
use app\Utils\WxPayUtil;
use function GuzzleHttp\Psr7\str;
use think\facade\Db;
use think\response\Json;

class UserCenter extends AppController
{

    /**
     * @param Request $request
     * @param UserService $service
     * 用户个人中心
     */
    public function other(Request $request, UserService $service)
    {
        $user_id = (int)$this->users($request->param('user_id', 0), $this->user); //用户ID
        $toid = $request->param('user_id', 0);
        $user = UserModel::where('id', $user_id)->find(); //获取用户数据信息
        $res = $service->userCenter($user_id, $user);
        $res['is_follow'] = $this->is_follow($this->user, $toid);
        return Util::returnJson(self::CODE_SUCCESS, $res, '获取成功');
    }

    /**
     * 判断是否关注
     */
    protected function is_follow($user, $toid): bool
    {
        if (!$user) return false;
        $sns_info = SnsModel::where(['sns_frommid' => $user['id'], 'sns_tomid' => $toid])->find();
        try {
            if ($sns_info) {
                return true;
            } else {
                return false;
            }
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * @param Request $request
     * @param VideoService $service
     * 视频列表
     */
    public function video(Request $request, VideoService $service)
    {
        $user_id = (int)$this->users($request->param('user_id', 0), $this->user); //用户ID
        $label = $request->param('label', ''); //视频标签
        $page = (int)$request->param('page', 1);
        $limit = (int)$request->param('limit', 8);
        $type = (int)$request->param('type', 1); // 1作品 2动态 3喜欢 more:作品
        $list = $service->userCenterList($label, $user_id, $type, $page, $limit, $request->param('user_id', 0));
        return Util::returnJson(self::CODE_SUCCESS, $list, '获取数据列表成功');
    }

    /**
     *设置  头像 昵称 地区
     */
    public function sets(Request $request)
    {
        $param = $request->param();
        if (empty($param['head_img']) && empty($param['nickname']) && empty($param['city'])) return Util::returnJson(self::CODE_ERROR, null, "请上传参数");
        $data = [
            'up_time' => date("Y-m-d H:i:s", time()),
        ];
        if (!empty($param['head_img'])) {
            $data['head_img'] = $param['head_img'];
        }
        if (!empty($param['nickname'])) {
            $data['nickname'] = $param['nickname'];
        }
        if (!empty($param['city'])) {
            $data['city'] = $param['city'];
        }
        $res = UserModel::updateUserWhere(['id' => $this->user['id']], $data);
        if ($res) {
            return Util::returnJson(self::CODE_SUCCESS, null, '设置成功');
        } else {
            return Util::returnJson(self::CODE_ERROR, null, "设置失败");
        }
    }

    /**
     * 关注列表
     */
    public function my_follow(Request $request, UserService $service)
    {
        $limit = (int)$request->param('limit', 10);
        $page = (int)$request->param('page', 1);
        $user_id = (int)$this->users($request->param('user_id', 0), $this->user); //用户ID
        $data = $service->follow($user_id, $page, $limit);
        return Util::returnPageJson(self::CODE_SUCCESS, $data['count'], $data['list'], '获取成功');
        //return Util::returnJson(self::CODE_SUCCESS, $service->follow($user_id), '获取成功');
    }

    /**
     * 老师身份商品列表
     */
    public function user_goods_lists(Request $request, UserService $service)
    {
        $user_id = $this->users($request->param('user_id', 0), $this->user);
        $page = (int)$request->get('page', 1);
        $limit = (int)$request->get('limit', 10);
        $good_name = (string)$request->get('good_name');
        $cate_id = (int)$request->get('cate_id');
        $is_recommend = (int)$request->get('is_recommend', 0);
        $is_bargain = (int)$request->get('is_bargain', 0);
        $is_composite = (int)$request->get('is_composite', 0);
        $is_price = (int)$request->get('is_price');
        $is_sales = (int)$request->get('is_sales');
        $lists = $service->goods_lists($user_id, $good_name, $cate_id, $is_recommend, $is_bargain, $is_composite, $is_price, $is_sales, $page, $limit);
        return Util::returnPageJson(self::CODE_SUCCESS, $lists['count'], $lists['data'], "");
    }

    /**
     * @param Request $request
     * @param UserService $service
     * @return array
     * 个人中心课程列表
     */
    public function curriculum_lists(Request $request, UserService $service)
    {
        $user_id = $this->users($request->param('user_id', 0), $this->user);
        $res = $service->curriculum_lists($user_id, $request->param());
        return Util::returnPageJson(self::CODE_SUCCESS, $res['count'], $res['list'], "");

    }

    /**
     * @param UserService $service
     * @return array
     * 老师入驻详情板块
     */
    public function teacher(UserService $service)
    {
        return Util::returnJson(self::CODE_SUCCESS, $service->teacher(), "");
    }

    /**
     * 是否是老师
     */
    public function is_teacher(Request $request)
    {
        $user = $this->user;
        if (!$user) return Util::returnJson(self::CODE_LOGIN, [], "token无效");
        $info = UserModel::where('id', $user['id'])->field('id,identity')->find();
        return Util::returnJson(self::CODE_SUCCESS, $info, "获取成功");
    }

    /**
     * 获取老师的线上课程
     */
    public function teacher_courses(Request $request, UserService $service)
    {
        $user_id = $this->users($request->param('user_id', 0), $this->user);
        $list = $service->teacher_courses_list($request->param(), $user_id);
        return Util::returnPageJson(self::CODE_SUCCESS, $list['count'], $list['list'], "");
    }

    /**
     * 用户粉丝
     */
    public function user_fans(Request $request, UserService $service)
    {
        $limit = (int)$request->param('limit', 10);
        $page = (int)$request->param('page', 1);
        $user_id = (int)$this->users($request->param('user_id', 0), $this->user); //用户ID
        $data = $service->fans($user_id, $page, $limit);
        return Util::returnPageJson(self::CODE_SUCCESS, $data['count'], $data['list'], '获取成功');
        // return Util::returnJson(self::CODE_SUCCESS, $service->fans($user_id), '获取成功');
    }

    /**
     * @param WechatPayUtil $payUtil
     * @return array
     * 测试
     */
    public function cheshi(WechatPayUtil $payUtil)
    {
        return Util::returnJson(self::CODE_SUCCESS, $payUtil->wx_pay_ceshi(), "获取成功");
    }
}